package com.smarthome.web.property.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.dictionary.DeviceTypeService;
import com.smarthome.domain.bean.ShEquipmentType;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

@Controller
@RequestMapping(value = "/deviceType")
public class DeviceTypeController {
	@Resource
	private DeviceTypeService deviceTypeService;
	
	/**
	 * 加载分页对象
	 * @param deviceType 查询条件
	 * @param page 分页条件
	 * @return
	 */
	@RequestMapping("/dataGrid")
    @ResponseBody
    public DataGrid dataGrid(ShEquipmentType deviceType, PageHelper page) {
        return deviceTypeService.queryDatagrid(deviceType, page);
    }
	
	/**
     * 跳转到设备类型添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/system/dictionary/deviceTypeAdd";
    }
    
    /**
     * 添加设备类型
     * 
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ShEquipmentType deviceType) {
    	if(StringUtils.isBlank(deviceType.getEquipmentTypeCode())){
    		return Result.failure("请选择设备类型【唯一标识】值！");
    	}
    	//判断是否已经存在  2015-10-10 by szc  
    	ShEquipmentType deviceType_new = new ShEquipmentType ();
    	deviceType_new.setEquipmentTypeCode(deviceType.getEquipmentTypeCode());
    	List<ShEquipmentType> list = deviceTypeService.queryDeviceTypeByCondition(deviceType_new);
    	if(list!=null && list.size()>0){
    		return Result.failure("所选设备类型【唯一标识】值已存在！");
    	}
    	deviceTypeService.add(deviceType);
        return Result.success("添加成功！");
    }
    
    /**
     * 跳转到设备类型修改页面
     * 
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request,Long id) {
    	ShEquipmentType deviceType=deviceTypeService.queryDeviceTypeById(id);
    	request.setAttribute("deviceType", deviceType);
        return "/system/dictionary/deviceTypeEdit";
    }
    
    /**
     * 修改设备类型
     * 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(ShEquipmentType deviceType) {
    	//判断是否已经存在  2015-10-10 by szc  
    	if(StringUtils.isBlank(deviceType.getEquipmentTypeCode())){
    		return Result.failure("请选择设备类型【唯一标识】值！");
    	}
    	deviceTypeService.edit(deviceType);
        return Result.success("修改成功！");
    }
    
    /**
     * 删除设备类型
     * 
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Long id) {
    	deviceTypeService.delete(id);
        return Result.success("删除成功！");
    }
    
    /**
     * 得到设备类型下拉选项
     * @return
     */
    @RequestMapping("/getComboData")
    @ResponseBody
    public Map<String,Object> getComboData() {
    	Map<String,Object> result = new HashMap<>();
    	List<Combobox> equipmentType = deviceTypeService.getDeviceTypeCombobox(false);
    	//List<Combobox> results = equipmentCommLogService.results(false);
    	result.put("equipmentType", equipmentType);
    	//result.put("results", results);
    	return result;
    }
    
    
}
